//
//  OutViewTest.m
//  ZHYYView
//
//  Created by MT-SW-005 on 2021/9/18.
//  Copyright © 2021 yuan zhang. All rights reserved.
//

#import "OutViewTest.h"
#import "Feed.h"
#import "FeedItem.h"


@interface OutViewTest()<NSOutlineViewDataSource,NSOutlineViewDelegate>

@end

@implementation OutViewTest
{
    NSOutlineView *_outLine;
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

-(id)initWithFrame:(NSRect)frameRect{
    self=[super initWithFrame: frameRect];
    if (self) {
        [self creatOutView];
    }
    return self;
}


-(void)creatOutView{
    
    NSScrollView * scrollView    = [[NSScrollView alloc] init];
    scrollView.hasVerticalScroller  = YES;
    scrollView.frame = self.bounds;
    [self addSubview:scrollView];
    _outLine = [[NSOutlineView alloc]initWithFrame:self.bounds];
    _outLine.allowsColumnResizing =YES;
     _outLine.gridStyleMask=NSTableViewSolidVerticalGridLineMask;//竖直分割线
    _outLine.usesAlternatingRowBackgroundColors = YES;
    
    
    NSTableColumn *tableColumn1 = [[NSTableColumn alloc] initWithIdentifier:@"c1"];
    NSTableColumn *tableColumn2 = [[NSTableColumn alloc] initWithIdentifier:@"c2"];
    NSTableColumn *tableColumn3 = [[NSTableColumn alloc] initWithIdentifier:@"c3"];
    NSTableColumn *tableColumn4 = [[NSTableColumn alloc] initWithIdentifier:@"c4"];
    NSTableColumn *tableColumn5 = [[NSTableColumn alloc] initWithIdentifier:@"c5"];
    NSTableColumn *tableColumn6 = [[NSTableColumn alloc] initWithIdentifier:@"c6"];
    
    tableColumn1.title=@"ID";
    tableColumn2.title=@"ITEM";
    tableColumn3.title=@"DUT1";
    tableColumn4.title=@"DUT2";
    tableColumn5.title=@"DUT3";
    tableColumn6.title=@"DUT4";
    
    tableColumn1.width = self.frame.size.width /12 *1;
    tableColumn2.width = self.frame.size.width /12 *3;
    tableColumn3.width = self.frame.size.width /12 *2;
    tableColumn4.width = self.frame.size.width /12 *2;
    tableColumn5.width = self.frame.size.width /12 *2;
    tableColumn6.width = self.frame.size.width /12 *2;
    
    [_outLine addTableColumn:tableColumn1];
    [_outLine addTableColumn:tableColumn2];
    [_outLine addTableColumn:tableColumn3];
    [_outLine addTableColumn:tableColumn4];
    [_outLine addTableColumn:tableColumn5];
    [_outLine addTableColumn:tableColumn6];
    
    _outLine.delegate = self;
    _outLine.dataSource = self;
    scrollView.contentView.documentView = _outLine;
    [self loadData];


}
-(void)loadData{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"plist"];
        if (filePath) {
            self.feeds = [Feed feedList:filePath];
            NSLog(@"feeds: %@", self.feeds);
            [_outLine reloadData];
    }
}

#pragma mark - NSOutlineViewDataSource

 //每一层级节点包含的下一级节点的数量
 //当item为空时表示根节点
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
    if (item) {
        Feed *feed = (Feed *)item;
        NSLog(@"====***==== feed.children.count:%ld ======",feed.children.count);
        return feed.children.count;
    } else {
        NSLog(@"====***==== self.feeds.countt:%ld",self.feeds.count);
        return self.feeds.count;
    }
}

//每一层级节点的模型对象为item时,根据item获取子节点模型。item为nil空时表示获取顶级节点模型。
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
    Feed *feed = (Feed *)item;
    if (feed) {
        return feed.children[index];
    } else {
        return self.feeds[index];
    }
}


//设置每个数据载体对应的具体数据，根据节点模型对象item,更新节点视图
-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item{
    if ([item isKindOfClass:[Feed class]]){
//        NSLog(@"===== %@",item);
        Feed *feed = (Feed *)item;
        if ([tableColumn.identifier isEqualToString:@"c1"]) {
//            NSLog(@"%@",feed.ID);
            return feed.ID;
        }else if([tableColumn.identifier isEqualToString:@"c2"]){
            return feed.name;
        }else if([tableColumn.identifier isEqualToString:@"c3"]){
            return feed.DUT1;
        } else if([tableColumn.identifier isEqualToString:@"c4"]){
            return feed.DUT2;
        }else if([tableColumn.identifier isEqualToString:@"c5"]){
            return feed.DUT3;
        }else if([tableColumn.identifier isEqualToString:@"c6"]){
            return feed.DUT4;
        }else {
            return @"---";
        }

    }else if ([item isKindOfClass:[FeedItem class]]) {
//        NSLog(@"==== %@",item);
        FeedItem *feedItem = (FeedItem *)item;
        if ([tableColumn.identifier isEqualToString:@"c1"]) {
            return feedItem.ID;
        }else if([tableColumn.identifier isEqualToString:@"c2"]){
            return feedItem.itemName;
        }else if([tableColumn.identifier isEqualToString:@"c3"]){
            return feedItem.DUT1;
        }else if([tableColumn.identifier isEqualToString:@"c4"]){
            return feedItem.DUT2;
        }else if([tableColumn.identifier isEqualToString:@"c5"]){
            return feedItem.DUT3;
        }else if([tableColumn.identifier isEqualToString:@"c6"]){
            return feedItem.DUT4;
        }else{
            return @"---";
        }

    }else{
        return @"NONO";
    }

}

//节点是否可以打开/关闭
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    if ([item isKindOfClass:[Feed class]]) {
        Feed *feed = (Feed *)item;
        return feed.children.count > 0;
    } else {
        return NO;
    }
}




-(void)changeDUT:(int)dut Data:(NSString*)data ID:(NSString*)itemID SubID:(NSString*)subID{
    if (dut == 1) {
        for (Feed *item in self.feeds) {
            if ([item.ID isEqualToString:itemID]) {
                BOOL flag =YES;
                NSMutableArray *subItem =item.children;
                for (FeedItem *fitem  in subItem) {
//                    NSLog(@"%@",fitem.description);
                    if ([fitem.ID isEqualToString:subID]) {
                        fitem.DUT1 =data;
                        if ([data isEqualToString:@"fail"]) {
                            flag=NO;
                        }
                    }
                }
            }
        }
    }
    
    if (dut == 2) {
        for (Feed *item in self.feeds) {
            if ([item.ID isEqualToString:itemID]) {
                BOOL flag =YES;
                if (!flag) {
                    item.DUT2=@"fail";
                }else {
                    item.DUT2=@"pass";
                }
                NSMutableArray *subItem =item.children;
                for (FeedItem *fitem  in subItem) {
                    NSLog(@"%@",fitem.description);
                    if ([fitem.ID isEqualToString:subID]) {
                        fitem.DUT2 =data;
                        if ([data isEqualToString:@"fail"]) {
                            flag=NO;
                        }
                    }
                }
            }
        }
    }
    
    if (dut == 3) {
        for (Feed *item in self.feeds) {
            if ([item.ID isEqualToString:itemID]) {
                BOOL flag =YES;
                if (!flag) {
                    item.DUT3=@"fail";
                }else {
                    item.DUT3=@"pass";
                }
                NSMutableArray *subItem =item.children;
                for (FeedItem *fitem  in subItem) {
                    NSLog(@"%@",fitem.description);
                    if ([fitem.ID isEqualToString:subID]) {
                        fitem.DUT3 =data;
                        if ([data isEqualToString:@"fail"]) {
                            flag=NO;
                        }
                    }
                }
            }
        }
    }
    
    if (dut == 4) {
        for (Feed *item in self.feeds) {
            if ([item.ID isEqualToString:itemID]) {
                BOOL flag =YES;
                if (!flag) {
                    item.DUT4=@"fail";
                }else {
                    item.DUT4=@"pass";
                }
                NSMutableArray *subItem =item.children;
                for (FeedItem *fitem  in subItem) {
                    NSLog(@"%@",fitem.description);
                    if ([fitem.ID isEqualToString:subID]) {
                        fitem.DUT4 =data;
                        if ([data isEqualToString:@"fail"]) {
                            flag=NO;
                        }
                    }
                }
            }
        }
    }
}


//重新加载数据
-(void)reloadDataOut{
//    NSLog(@"reload all Data");
    [_outLine reloadData];
}





#pragma mark - NSOutlineViewDelegate
/*
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    
    NSTableCellView *view;
    if ([item isKindOfClass:[Feed class]]){
        Feed *feed = (Feed *)item;
        NSLog(@"1 === feed:%@",feed);
        if ([tableColumn.identifier isEqualToString:@"c2"]) {
            view = (NSTableCellView *)[outlineView makeViewWithIdentifier:@"DateCell" owner:self];
            NSTextField *textField = view.textField;
            if (textField) {
                NSLog(@"1 === textfiled 11 ");
                textField.stringValue = @"666666";
                [textField sizeToFit];
            }
        } else {
            view = (NSTableCellView *)[outlineView makeViewWithIdentifier:@"FeedCell" owner:self];
            NSTextField *textField = view.textField;
            if (textField) {
                NSLog(@"1 === textfiled 12 ");
                textField.stringValue = feed.name;
                [textField sizeToFit];
            }
        }
    } else if ([item isKindOfClass:[FeedItem class]]) {
        FeedItem *feedItem = (FeedItem *)item;
        NSLog(@"2 == feed item:%@",feedItem);
        if ([tableColumn.identifier isEqualToString:@"c2"]) {
            view = (NSTableCellView *)[outlineView makeViewWithIdentifier:@"DateCell" owner:self];
            NSTextField *textField = view.textField;
            if (textField) {
                textField.stringValue = [self.dateFormatter stringFromDate:feedItem.publishingDate];
                [textField sizeToFit];
            }
        } else {
            view = (NSTableCellView *)[outlineView makeViewWithIdentifier:@"FeedItemCell" owner:self];
            NSTextField *textField = view.textField;
            if (textField) {
                textField.stringValue = feedItem.title;
                [textField sizeToFit];
            }
        }
    }
    return view;
}

 */



@end





















